package com.intelligence.pda.ui.modifyphone;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.intelligence.androidlibrary.utils.SharePreferenceUtils;
import com.intelligence.pda.R;
import com.intelligence.pda.parameter.ResetPhonenumberParameter;
import com.intelligence.pda.parameter.VerifycodeParameter;
import com.intelligence.pda.retrofit.ApiCallback;
import com.intelligence.pda.ui.BaseActivity;
import com.intelligence.pda.ui.modifyphone.model.VerifycodeModel;
import com.intelligence.pda.ui.signout.model.LoginOutModel;
import com.intelligence.pda.utils.AppConstants;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;

public class ModifyPhoneActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.ll_toolbar_return)
    LinearLayout ll_toolbar_return;
    @Bind(R.id.tv_toolbar_title)
    TextView tv_toolbar_title;
    @Bind(R.id.tv_determine)
    TextView tv_determine;
    @Bind(R.id.tv_number_used)
    TextView tv_number_used;
    @Bind(R.id.et_number_new)
    EditText et_number_new;
    @Bind(R.id.et_verification)
    EditText et_verification;
    @Bind(R.id.bt_verification)
    Button bt_verification;
    private MyCountDownTimer myCountDownTimer;
    private String verifyCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_phone);
        tv_toolbar_title.setText("修改手机号");
        myCountDownTimer = new MyCountDownTimer(60000, 1000);
        initView();
        listener();
    }

    private void listener() {
        ll_toolbar_return.setOnClickListener(this);
        bt_verification.setOnClickListener(this);
        tv_determine.setOnClickListener(this);
    }

    private void initView() {
        String mobile = SharePreferenceUtils.getString(AppConstants.MOBILE);
        if (mobile.length() == 11) {
            String maskNumber = mobile.substring(0, 3) + "****" + mobile.substring(7, mobile.length());
            tv_number_used.setText(maskNumber);
        }else{
            tv_number_used.setText("-");
        }

    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_toolbar_return://返回
                myCountDownTimer.cancel();
                finish();
                break;
            case R.id.tv_determine://确定
                if (getdetermine()) {
                    getResetPhonenumber();
                }
                break;
            case R.id.bt_verification:
                if (getverification()) {
                    verifyCode = "";
                    myCountDownTimer.start();
                    getVerifycode();

                }
                break;

        }
    }

    public boolean getverification() {
        if (TextUtils.isEmpty(et_number_new.getText().toString())) {
            toastShow("请输入手机号");
            return false;
        }
        if (!isMobileNum(et_number_new.getText().toString())) {
            toastShow("请输入正确手机号");
            return false;
        }
        return true;
    }

    public boolean getdetermine() {
        if (TextUtils.isEmpty(et_number_new.getText().toString())) {
            toastShow("请输入手机号");
            return false;
        }
        if (!isMobileNum(et_number_new.getText().toString())) {
            toastShow("请输入正确手机号");
            return false;
        }
        if (TextUtils.isEmpty(et_verification.getText().toString())) {
            toastShow("请输入验证码");
            return false;
        }
        if (verifyCode.equals(et_verification.getText().toString())) {
            toastShow("验证码不正确");
            et_number_new.setText("");
            return false;
        }
        return true;
    }

    /**
     * 检查是否是电话号码
     *
     * @return
     */
    public static boolean isMobileNum(String mobiles) {
        Pattern p = Pattern
                .compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    //复写倒计时
    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程
        @Override
        public void onTick(long l) {
            //防止计时过程中重复点击
            bt_verification.setClickable(false);
            bt_verification.setText(l / 1000 + "s");

        }

        //计时完毕的方法
        @Override
        public void onFinish() {
            //重新给Button设置文字
            bt_verification.setText("重新获取验证码");
            //设置可点击
            bt_verification.setClickable(true);
        }
    }

    public void getVerifycode() {
        //获取手机号
        VerifycodeParameter verifycodeParameter = new VerifycodeParameter();
        verifycodeParameter.setMobile(et_number_new.getText().toString());
        verifycodeParameter.setSmsType(AppConstants.SMS_MODIFPSWD);
        verifycodeParameter.setUserType("");
        addSubscription(apiStores.getVerifycode(verifycodeParameter),
                new ApiCallback<VerifycodeModel>() {
                    @Override
                    public void onSuccess(VerifycodeModel model) {
                        verifyCode = model.getData().getVerifyCode();
                    }

                    @Override
                    public void onFailure(VerifycodeModel model, String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }


                    @Override
                    public void onFinish() {
                    }

                });

    }

    public void getResetPhonenumber() {
        //获取手机号
        showProgressDialog();
        ResetPhonenumberParameter resetPhonenumberParameter = new ResetPhonenumberParameter();
        resetPhonenumberParameter.setMobile(et_number_new.getText().toString());
        resetPhonenumberParameter.setVerifyCode(verifyCode);
        resetPhonenumberParameter.setBasicStr(SharePreferenceUtils.getString(AppConstants.BASIC_STR));
        addSubscription(apiStores.getResetPhonenumber(resetPhonenumberParameter),
                new ApiCallback<LoginOutModel>() {
                    @Override
                    public void onSuccess(LoginOutModel model) {
                        dismissProgressDialog();
                        SharePreferenceUtils.putString(AppConstants.MOBILE, et_number_new.getText().toString());
                        toastShow("手机号码修改成功");
                        finish();
                    }

                    @Override
                    public void onFailure(LoginOutModel model, String msg) {
                        toastShow(msg);
                        dismissProgressDialog();
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                        dismissProgressDialog();
                    }


                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }

                });

    }
}
